package com.wise.aip.service.impl;

import java.util.Map;

import com.wise.aip.kit.WiseDataKit;
import com.wise.aip.service.BaseDataService;
import com.wise.aip.template.URLTemplate;
import com.wise.aip.token.TokenKit;

/**
 * 
 * Title: BankAccountDaoImpl <br>
 * Description: 银行账号业务层 ItemClass2024
 * 
 * @author yacong_liu Email:2682505646@qq.com
 * @date 2019年4月13日
 */
public class BankAccountServiceImpl extends BaseDataService {
    /**
     * 
     * <p>
     * Title: save
     * </p>
     * <p>
     * Description: 新增银行账号
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:BankAccount
     * @param params 参数 key: name number acntNumber(银行账号) actName(账户名称) acntBranch(开户行)
     * @return
     * @throws Exception
     */
    public String saveBankAccount(String gatewayHost, String authorityCode, String itemClassCode,
            Map<String, String> params) throws Exception {

        StringBuffer log = new StringBuffer();
        String name = "";
        String number = "";
        // 银行账号接口类型
        String bankTypeID = "999";
        String bankTypeName = "其他银行";
        String acntNumber = ""; // 银行账号
        String actName = ""; // 账户名称
        String acntBranch = ""; // 开户行
        if (params.size() > 0) {
            name = params.get("name");
            number = params.get("number");
            acntNumber = params.get("acntNumber");
            actName = params.get("actName");
            acntBranch = params.get("acntBranch");
        }

        log.append("**************" + this.getClass().getName() + "_saveBankAccount （新增银行账号）START******\n");
        log.append("**************入参名称：" + name + " 编码：" + number);

        String requestURL = URLTemplate.save(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String param = "{\"Data\":{\"FName\":'" + name + "',\"FNumber\":'" + number
                + "',\"FBankTypeID\":{ \"FID\": '" + bankTypeID + "',\"FName\":'" + bankTypeName
                + "'},\"FAcntNumber\":'" + acntNumber + "',\"FAcntName\":'" + actName + "',\"FAcntBranch\":'"
                + acntBranch + "'}}";
        String saveRes = super.save(requestURL, param);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + param);
        log.append("\n*************响应结果：" + saveRes);
        log.append("\n**************************************新增银行账号END******************");

        System.out.println(log.toString());
        return saveRes;

    }

    /**
     * 
     * <p>
     * Title: updateBankAccount
     * </p>
     * <p>
     * Description: 修改银行账号
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:BankAccount
     * @param params 参数 key: name number oldNumber
     * @return
     * @throws Exception
     */
    public String updateBankAccount(String gatewayHost, String authorityCode, String itemClassCode,
            Map<String, String> params) throws Exception {

        StringBuffer log = new StringBuffer();
        String name = "";
        String number = "";
        String oldNumber = "";
        // 银行账号接口类型
        String bankTypeID = "999";
        String bankTypeName = "其他银行";
        String acntNumber = ""; // 银行账号
        String actName = ""; // 账户名称
        String acntBranch = ""; // 开户行
        if (params.size() > 0) {
            name = params.get("name");
            number = params.get("number");
            oldNumber = params.get("oldNumber");
        }

        log.append("**************" + this.getClass().getName() + "_updateBankAccount （修改银行账号）START******\n");
        log.append("**************入参名称：" + name + " 编码：" + number);

        String token = TokenKit.getAccessToken(gatewayHost, authorityCode);
        // 根据编码查出目前的信息
        Map<String, String> map = WiseDataKit.getNowBankAccountInfoByNumber(gatewayHost, itemClassCode, token,
                oldNumber);
        if (map != null && map.size() > 0) {
            bankTypeID = map.get("BankTypeID");
            bankTypeName = map.get("BankTypeName");
            acntNumber = map.get("FAcntNumber");
            actName = map.get("FAcntName");
            acntBranch = map.get("FAcntBranch");
        }

        String requestURL = URLTemplate.update(gatewayHost, itemClassCode, token);
        String param = "{\"Data\":{\"FNumber\":'" + oldNumber + "',\"Data\":{\"FName\":'" + name
                + "',\"FNumber\":'" + number + "',\"FBankTypeID\":{ \"FID\": '" + bankTypeID + "',\"FName\":'"
                + bankTypeName + "'},\"FAcntNumber\":'" + acntNumber + "',\"FAcntName\":'" + actName
                + "',\"FAcntBranch\":'" + acntBranch + "'}}}";
        String updateRes = super.update(requestURL, param);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + param);
        log.append("\n*************响应结果：" + updateRes);
        log.append("\n**************************************修改银行账号END******************");

        System.out.println(log.toString());
        return updateRes;

    }

    /**
     * 
     * <p>
     * Title: deleteBankAccount
     * </p>
     * <p>
     * Description: 删除银行账号
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:BankAccount
     * @param number 需删除的编码
     * @return
     * @throws Exception
     */
    public String deleteBankAccount(String gatewayHost, String authorityCode, String itemClassCode,
            String number) throws Exception {

        StringBuffer log = new StringBuffer();
        log.append("**************" + this.getClass().getName() + "_deleteBankAccount （删除银行账号）START******\n");
        log.append("**************入参编码：" + number);

        String requestURL = URLTemplate.delete(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String deleteRes = super.deleteOrforbid(requestURL, number);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + number);
        log.append("\n*************响应结果：" + deleteRes);
        log.append("\n**************************************删除银行账号END******************");

        System.out.println(log.toString());
        return deleteRes;
    }

}
